package com.guandi.geekbrowser.primary.repository;

import com.guandi.geekbrowser.primary.entity.TaskItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface TaskItemRepository extends JpaRepository<TaskItem, Long>, JpaSpecificationExecutor<TaskItem> {
    List<TaskItem> findAllByUserIdAndType(String userId, Integer type);

    TaskItem findAllByUserIdAndTypeAndId(String userId, Integer type,Long id);

    List<TaskItem> findAllByUserIdAndTypeAndTaskName(String userId, Integer type, String taskName);

    List<TaskItem> findAllByUserIdAndTypeAndTaskNameAndFinish(String userId, Integer type, String taskName,Boolean finish);

    List<TaskItem> findAllByUserIdAndTypeAndTaskNameAndFinishAndCountry(String userId, Integer type, String taskName,Boolean finish,String country);

    @Query(value = "update TaskItem l set l.notes = ?1 where l.id =?2 and l.userId = ?3")
    @Modifying
    @Transactional
    Integer updateLikedNotesByIdAndUserId(String notes, Long id, String userId);

    @Query(value = "update TaskItem l set l.url = ?1,l.asinCode=?2,l.totalCount=?3 where l.id =?4 and l.userId = ?5")
    @Modifying
    @Transactional
    Integer updateLikedByIdAndUserId(String url,String asinCode,Integer totalCount,Long id, String userId);

    @Transactional
    Integer deleteByIdAndUserId(Long id, String userId);

    Page<TaskItem> findAll(Specification<TaskItem> specification, Pageable pageable);



}
